package com.py.credit.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.py.credit.common.ServiceException;
import com.py.credit.dao.BatchFileDao;
import com.py.credit.dao.BatchLogDao;
import com.py.credit.dao.ColumnDao;
import com.py.credit.entity.*;
import com.py.credit.entity.common.LogEnum;
import com.py.credit.service.*;
import com.py.credit.service.excel.read.service.InspectDataStyleService;
import com.py.credit.utils.*;
import com.py.framework.core.common.Page;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 批次日志ServiceImpl
 *
 * @author jicm
 */
@Service
public class BatchLogServiceImpl implements BatchLogService {

    private static final Logger LOG = LoggerFactory.getLogger(BatchLogServiceImpl.class);

    private static final String CHECKREPEAT_NO = "N";

    @Autowired
    BatchLogDao batchLogDao;

    @Autowired
    ContentService contentService;

    @Autowired
    BatchFileDao batchFileDao;

    @Autowired
    ColumnDao columnDao;

    @Autowired
    WriteLogsService writeLogsService;

    @Autowired
    private TableService tableService;

    @Autowired
    @Qualifier("default")
    private InspectDataStyleService inspectDataStyleService;

    @Autowired
    private DataEntryService dataEntryService;

    @Autowired
    private StepLogService stepLogService;

    /**
     * 上传文件
     */

    @Override
    public String doUploadFile(Table table, MultipartFile file, SysUser userInfo, String absolutepath)
            throws Exception {

        String batchId = UuidUtil.getInstance().getUUid();
        // 文件存储路径
        StringBuilder pathFile = new StringBuilder(200);
        pathFile.append(File.separator);
        pathFile.append(userInfo.getId());
        pathFile.append(File.separator);
        pathFile.append(table.getResourceId());

        // 获取文件的后缀
        String fileType = null;
        if (file != null) {
            // 获取文件后缀
            if (file.getOriginalFilename().contains(".")) {
                fileType = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."),
                        file.getOriginalFilename().length());
            }
        } else {
            throw new ServiceException("上传的文件不得为空");// 抛出异常
        }
        if (!".xls".equalsIgnoreCase(fileType) && !".xlsx".equalsIgnoreCase(fileType)) {
            throw new ServiceException("上传的文件格式不支持。");
        }
        // 先存储文件
        fileUpload(file, absolutepath + "/upload" + pathFile.toString(), batchId.toString(), fileType);
        LOG.debug("上传文件存储路径：{}", absolutepath + "/upload" + pathFile.toString());
        // 添加文件记录
        batchFileDao.addBatchFile(batchId.toString(), batchId + fileType, pathFile.toString() + File.separator, "");
        // 添加批次日志
        batchLogDao.addBatchLog(batchId.toString(), table.getTableId(), "0", table.getRootId(), userInfo.getOfficeId(),
                userInfo.getId(), LogEnum.WCL, null);

        return batchId.toString();

    }

    /**
     * 上传文件
     *
     * @param file        文件
     * @param request
     * @param orgBm       所属编码
     * @param fileName    文件名称
     * @param tableNameEn 文件英文名
     * @throws IOException
     * @throws IllegalStateException
     */
    private void fileUpload(MultipartFile file, String pathFile, String batchId, String fileType)
            throws IllegalStateException, IOException {
        File uploadFile = null;

        File dirPath = new File(pathFile);
        uploadFile = new File(pathFile + File.separator + batchId + fileType);
        if (!dirPath.exists()) {
            uploadFile.mkdirs();
        }
        file.transferTo(uploadFile);

    }

    /**
     * @param batchId
     * @return
     * @Function 根据权限限制获取批次信息
     * @FileName BatchLogServiceImpl.java
     * @MethodName selectPermissionLimit
     * @Date 2018年12月3日 上午9:53:23
     * @Author Min DongXv
     */
    @Override
    public BatchLog selectPermissionLimit(String batchId, SysUser userInfo) {
        return batchLogDao.selectPermissionLimit(batchId, userInfo.getOfficeId(), userInfo.getAreaId(),
                userInfo.getUserType());
    }

    /**
     * @param batchId
     * @return
     * @Function 根据批次ID获取批次信息
     * @FileName BatchLogServiceImpl.java
     * @MethodName select
     * @Date 2018年12月3日 上午9:53:23
     * @Author Min DongXv
     */
    @Override
    public BatchLog select(String batchId) {
        return batchLogDao.select(batchId);
    }

    @Override
    public void fileDownload(String batchId, HttpServletResponse response, String absolutepath) {
        BatchFile batchFile = batchFileDao.selectBatchFile(batchId);
        String path = absolutepath + "/upload" + batchFile.getFpath() + batchFile.getFname();
        FileUtil.downloadFile(path, response);
    }

    @Override
    public void logDownload(String batchId, HttpServletResponse response, String absolutepath) {
        String logPath = writeLogsService.readLogFilePath(batchId, absolutepath);
        FileUtil.downloadFile(logPath, response);
    }

    @Override
    public List<Table> tableForDeptList(Long regionId, Long officeId, String code) {
        // TODO 根据当前登录用户，获取该用户部门所订阅的目录版本，如果没有，则选用启用的公共目录
        return batchLogDao.selectTableForDept(regionId, officeId, code);
    }

    @Override
    public List<BatchFile> getUntreatedFiles() {
        return batchFileDao.getUntreatedFiles();
    }

    @Override
    public void modify(String batchId, String clz) {
        batchLogDao.modifyStatus(batchId, clz);

    }

    @Override
    public void modifyBatchLog(String batchId, String result, int tcount, int scount, int fcount, int rcount,
                               int waitCount, String status) {
        batchLogDao.modifyBatchLog(batchId, result, tcount, scount, fcount, rcount, waitCount, status);
    }

    @Override
    public List<Map<String, Object>> getByConditionForPage(List<String> columns, String[] additionalColumns,
                                                           String conditionStr, Page page, SysUser userInfo) {
        List<String> conditions = SQLBuilder.createConditionSql(conditionStr);
        Long topDeptId = userInfo.getOfficeId();
        String pIds = userInfo.getAllParentId();
        if (StringUtils.isNotBlank(pIds)) {
            String t = StringUtils.split(pIds, ",")[0];
            topDeptId = StringUtils.isNoneBlank(t) ? Long.valueOf(t) : topDeptId;
        }
        String roleName = userInfo.getRoleName();
        List<Map<String, Object>> batchLogs = new ArrayList<Map<String,Object>>();
        if (roleName.equals("系统管理员")) {
            batchLogs  = batchLogDao.selectByConditionForPage(columns, additionalColumns,
                    conditions, page, topDeptId, null, userInfo.getUserType(), null);
        }else if(roleName.equals("部门管理员")) {
            batchLogs  = batchLogDao.selectByConditionForPage(columns, additionalColumns,
                    conditions, page, topDeptId, null, userInfo.getUserType(), userInfo.getOfficeId());
        }else {
            batchLogs  = batchLogDao.selectByConditionForPage(columns, additionalColumns,
                    conditions, page, topDeptId, userInfo.getId(), userInfo.getUserType(), userInfo.getOfficeId());
        }
        return batchLogs;
    }

    @Override
    public List<Map<String, Object>> getDoubleByConditionForPage(List<String> columns, String[] additionalColumns, String conditionStr, Page page, SysUser userInfo) {
        List<String> conditions = SQLBuilder.createConditionSql(conditionStr);
        Long topDeptId = userInfo.getOfficeId();
        String pIds = userInfo.getAllParentId();
        if (StringUtils.isNotBlank(pIds)) {
            String t = StringUtils.split(pIds, ",")[0];
            topDeptId = StringUtils.isNoneBlank(t) ? Long.valueOf(t) : topDeptId;
        }
        String roleName = userInfo.getRoleName();
        List<Map<String, Object>> batchLogs = new ArrayList<Map<String,Object>>();
        if (roleName.equals("系统管理员")) {
            batchLogs  = batchLogDao.selectDoubleByConditionForPage(columns, additionalColumns,
                    conditions, page, topDeptId, null, userInfo.getUserType(), null);
        }else if(roleName.equals("部门管理员")) {
            batchLogs  = batchLogDao.selectDoubleByConditionForPage(columns, additionalColumns,
                    conditions, page, topDeptId, null, userInfo.getUserType(), userInfo.getOfficeId());
        }else {
            batchLogs  = batchLogDao.selectDoubleByConditionForPage(columns, additionalColumns,
                    conditions, page, topDeptId, userInfo.getId(), userInfo.getUserType(), userInfo.getOfficeId());
        }
        return batchLogs;
    }

    @Override
    public List<Map<String, Object>> getDoubleFrByConditionForPage(List<String> columns, String[] additionalColumns, String conditionStr, Page page, SysUser userInfo) {
        List<String> conditions = SQLBuilder.createConditionSql(conditionStr);
        Long topDeptId = userInfo.getOfficeId();
        String pIds = userInfo.getAllParentId();
        if (StringUtils.isNotBlank(pIds)) {
            String t = StringUtils.split(pIds, ",")[0];
            topDeptId = StringUtils.isNoneBlank(t) ? Long.valueOf(t) : topDeptId;
        }
        String roleName = userInfo.getRoleName();
        List<Map<String, Object>> batchLogs = new ArrayList<Map<String,Object>>();
        if (roleName.equals("系统管理员")) {
            batchLogs  = batchLogDao.selectDoubleFrByConditionForPage(columns, additionalColumns,
                    conditions, page, topDeptId, null, userInfo.getUserType(), null);
        }else if(roleName.equals("部门管理员")) {
            batchLogs  = batchLogDao.selectDoubleFrByConditionForPage(columns, additionalColumns,
                    conditions, page, topDeptId, null, userInfo.getUserType(), userInfo.getOfficeId());
        }else {
            batchLogs  = batchLogDao.selectDoubleFrByConditionForPage(columns, additionalColumns,
                    conditions, page, topDeptId, userInfo.getId(), userInfo.getUserType(), userInfo.getOfficeId());
        }
        return batchLogs;
    }

    @Override
    @Deprecated
    public List<Map<String, Object>> getByCondition(String deptId, String condition, List<String> columnEN) {

        List<String> conditions = SQLBuilder.createConditionSql(condition);

        return batchLogDao.selectByCondition(deptId, columnEN, conditions);
    }

    @Override
    public List<Map<String, Object>> getErrorDataByConditionForPage(List<String> columns, String[] additionalColumns,
                                                                    String conditionStr, String deptId, String code, String startTime, String endTime, String tableName,
                                                                    Page page, SysUser userInfo) {
        List<String> conditions = SQLBuilder.createConditionSql(conditionStr);
        Date day = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        if (startTime.equals("")) {
            // 开始时间默认为一个月前
            Calendar c = Calendar.getInstance();
            c.setTime(day);
            c.add(Calendar.MONTH, -1);
            startTime = df.format(c.getTime());
        }
        if (endTime.equals("")) {
            // 结束时间默认为当天
            endTime = df.format(day);
        }
        String pIds = userInfo.getAllParentId();
        Long topDeptId = userInfo.getOfficeId();
        if (StringUtils.isNotBlank(pIds)) {
            String t = StringUtils.split(pIds, ",")[0];
            topDeptId = StringUtils.isNoneBlank(t) ? Long.valueOf(t) : topDeptId;
        }
        if (userInfo.getUserType() > 1) {
            userInfo.setId(null);
        }
        List<Map<String, Object>> batchLogs = batchLogDao.getErrorDataByConditionForPage(columns, additionalColumns,
                conditions, userInfo.getId(), deptId, code, topDeptId, startTime, endTime, tableName, page,
                userInfo.getAreaId(), userInfo.getUserType(), userInfo.getOfficeId());
        return batchLogs;
    }

    @Override
    public List<Column> getColumnList(int tableId) {
        return columnDao.selectListByTableId(tableId);
    }

    @Override
    public List<Map<String, Object>> getLogQueryListByConditionForPage(List<String> columns, String[] additionalColumns,
                                                                       String conditionStr, Page page, SysUser userInfo) {
        List<String> conditions = SQLBuilder.createConditionSql(conditionStr);
        String pIds = userInfo.getAllParentId();
        Long topDeptId = userInfo.getOfficeId();
        if (StringUtils.isNotBlank(pIds)) {
            String t = StringUtils.split(pIds, ",")[0];
            topDeptId = StringUtils.isNoneBlank(t) ? Long.valueOf(t) : topDeptId;
        }
        List<Map<String, Object>> batchLogs = batchLogDao.selectLogQueryListByConditionForPage(columns,
                additionalColumns, conditions, page, topDeptId, userInfo.getAreaId(), userInfo.getUserType(),
                userInfo.getOfficeId());
        return batchLogs;
    }

    /**
     * @param condition
     * @param userInfo
     * @return
     * @Function 获取饼图数据
     * @FileName BatchLogServiceImpl.java
     * @MethodName getLogQueryBieByCondition
     * @Date 2018年6月4日 下午5:59:16
     * @Author Min DongXv
     */
    @Override
    public Map<String, Object> getLogQueryBieByCondition(String condition, SysUser userInfo) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        List<String> conditions = SQLBuilder.createConditionSql(condition);
        String pIds = userInfo.getAllParentId();
        Long topDeptId = userInfo.getOfficeId();
        if (StringUtils.isNotBlank(pIds)) {
            String t = StringUtils.split(pIds, ",")[0];
            topDeptId = StringUtils.isNoneBlank(t) ? Long.valueOf(t) : topDeptId;
        }
        List<Map<String, Object>> batchLogs = batchLogDao.getLogQueryBieByCondition(conditions, topDeptId,
                userInfo.getAreaId(), userInfo.getUserType(), userInfo.getOfficeId());
        result.put("importCount", 0);
        result.put("intfCount", 0);
        result.put("manualCount", 0);
        result.put("etlCount", 0);
        if (!CollectionUtils.isEmpty(batchLogs)) {
            for (Map<String, Object> map : batchLogs) {
                if (null != map && null != map.get("type")) {
                    String type = String.valueOf(map.get("type"));
                    int count = Integer.parseInt(String.valueOf(null == map.get("count") ? "0" : map.get("count")));
                    if ("0".equals(type)) {
                        // 导入数据
                        result.put("importCount", count);
                    } else if ("1".equals(type)) {
                        // 接口数据
                        result.put("intfCount", count);
                    } else if ("2".equals(type)) {
                        // 手工数据
                        result.put("manualCount", count);
                    } else if ("3".equals(type)) {
                        // etl数据
                        result.put("etlCount", count);
                    }
                }
            }
        }
        return result;
    }

    @Override
    public Map<String, Object> getLogQueryBar(String startTime, String endTime) {
        Date day = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String endDate = df.format(day);
        String date = DateUtils.testForDate();// 获取当前月的第一天
        if (startTime.equals("")) {
            startTime = date;
        }
        if (endTime.equals("")) {
            endTime = endDate;
        }
        List<Map<String, Object>> dataBar = batchLogDao.getLogQueryBar(startTime, endTime, "B");
        List<Map<String, Object>> dataLine = batchLogDao.getLogQueryBar(startTime, endTime, "L");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("startTime", Validater.dateTransformation(startTime));
        map.put("endTime", Validater.dateTransformation(endTime));
        map.put("bar", dataBar);
        map.put("line", dataLine);

        return map;
    }

    @Override
    public Map<String, Object> selectData(SysUser userInfo, String deptId, String code, String startTime,
                                          String endTime, String tableId) {
        DateUtil du = DateUtil.getInstance();
        if (startTime.equals("")) {
            startTime = du.dateToStr(du.getCurrUpMonthDate(), DateUtil.YYYY_MM_DD);
            // startTime = DateUtils.getDay(false, 0,
            // DateUtils.STANDADD_PATTERN);
        }
        if (endTime.equals("")) {
            // 结束时间默认为当天
            endTime = du.dateToStr(du.getCurrDate(), DateUtil.YYYY_MM_DD);
            // endTime = DateUtils.getDay(false, 1, DateUtils.STANDADD_PATTERN);
        }
        String pIds = userInfo.getAllParentId();
        Long topDeptId = userInfo.getOfficeId();
        if (StringUtils.isNotBlank(pIds)) {
            String t = StringUtils.split(pIds, ",")[0];
            topDeptId = StringUtils.isNoneBlank(t) ? Long.valueOf(t) : topDeptId;
        }
        Map<String, Object> map = batchLogDao.selectData(userInfo.getId(), deptId, code, startTime, endTime, tableId,
                userInfo.getAreaId(), topDeptId, userInfo.getUserType(), userInfo.getOfficeId());
        return map;
    }

    @Override
    public Map<String, Object> getCollectionInfo() {

        return batchLogDao.selectCollectionInfo();
    }

    @Override
    public List<Map<String, Object>> getCollectionAnalysis() {

        return batchLogDao.selectCollectionAnalysis();
    }

    @Override
    public List<Map<String, Object>> getDeptRkNum() {

        return batchLogDao.selectDeptRkNum();
    }

    @Override
    public List<BatchLog> getUnAssociatedBatch() {

        return batchLogDao.selectUnAssociatedBatch();
    }

    @Override
    public void updateBatchLogAssociationStatus(String id, String status) {
        batchLogDao.updateBatchLogAssociationStatus(id, status);

    }

    /**
     * @param deptId
     * @return
     * @Function 获取入库日志柱状图
     * @FileName BatchLogServiceImpl.java
     * @MethodName logQueryBarView
     * @Date 2018年5月29日 下午7:02:40
     * @Author Min DongXv
     */
    @Override
    public Map<String, Object> logQueryBarView(SysUser userInfo) {
        Map<String, Object> result = new HashMap<String, Object>(6);
        List<String> days = getDayListOfMonth();
        result.put("xAxis", days);
        Map<String, Object> importMap = new HashMap<String, Object>();
        Map<String, Object> etlMap = new HashMap<String, Object>();
        Map<String, Object> manualMap = new HashMap<String, Object>();
        Map<String, Object> intfMap = new HashMap<String, Object>();
        SimpleDateFormat df = new SimpleDateFormat(DateUtil.YYYY_MM_DD);
        Calendar calendar = Calendar.getInstance();
        // 设置为当月第一天
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        String startDate = df.format(calendar.getTime());
        // 设置为当月最后一天
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        String endDate = df.format(calendar.getTime());
        String pIds = userInfo.getAllParentId();
        Long topDeptId = userInfo.getOfficeId();
        if (StringUtils.isNotBlank(pIds)) {
            String t = StringUtils.split(pIds, ",")[0];
            topDeptId = StringUtils.isNoneBlank(t) ? Long.valueOf(t) : topDeptId;
        }
        // 获取导入批次统计数据
        List<Map<String, Object>> data = batchLogDao.logQueryBarView(startDate, endDate, topDeptId,
                userInfo.getAreaId(), userInfo.getUserType(), userInfo.getOfficeId());
        if (!CollectionUtils.isEmpty(data)) {
            for (Map<String, Object> map : data) {
                if (null != map) {
                    String type = String.valueOf(map.get("type"));
                    if ("0".equals(type)) {
                        // 导入数据
                        importMap.put(String.valueOf(map.get("endTime")),
                                Integer.parseInt(String.valueOf(map.get("tcount"))));
                    } else if ("1".equals(type)) {
                        // 接口数据
                        intfMap.put(String.valueOf(map.get("endTime")),
                                Integer.parseInt(String.valueOf(map.get("tcount"))));
                    } else if ("2".equals(type)) {
                        // 手工数据
                        manualMap.put(String.valueOf(map.get("endTime")),
                                Integer.parseInt(String.valueOf(map.get("tcount"))));
                    } else if ("3".equals(type)) {
                        // etl数据
                        etlMap.put(String.valueOf(map.get("endTime")),
                                Integer.parseInt(String.valueOf(map.get("tcount"))));
                    }
                }
            }
        }
        List<String> importData = new ArrayList<String>(days.size());
        List<String> etlData = new ArrayList<String>(days.size());
        List<String> manualData = new ArrayList<String>(days.size());
        List<String> intfData = new ArrayList<String>(days.size());
        List<String> totalData = new ArrayList<String>(days.size());
        // 每天数据填充，默认为0
        for (String day : days) {
            Integer total = 0;
            String count = (null == importMap.get(day) ? "0" : importMap.get(day).toString());
            total = total + Integer.parseInt(count);
            importData.add(count);
            count = (null == etlMap.get(day) ? "0" : etlMap.get(day).toString());
            total = total + Integer.parseInt(count);
            etlData.add(count);
            count = (null == manualMap.get(day) ? "0" : manualMap.get(day).toString());
            total = total + Integer.parseInt(count);
            manualData.add(count);
            count = (null == intfMap.get(day) ? "0" : intfMap.get(day).toString());
            total = total + Integer.parseInt(count);
            intfData.add(count);
            totalData.add(String.valueOf(total));
        }
        result.put("importData", importData);
        result.put("etlData", etlData);
        result.put("manualData", manualData);
        result.put("intfData", intfData);
        result.put("totalData", totalData);
        return result;
    }

    private List<String> getDayListOfMonth() {
        List<String> list = new ArrayList<String>();
        Calendar calendar = Calendar.getInstance();
        int month = calendar.get(Calendar.MONTH) + 1;// 月份
        int day = calendar.getActualMaximum(Calendar.DATE);
        for (int i = 1; i <= day; i++) {
            String date = "";
            if (month < 10) {
                date = "0" + month + "-";
            } else {
                date = month + "-";
            }
            if (i < 10) {
                date = date + "0" + i;
            } else {
                date = date + i;
            }
            list.add(date);
        }
        return list;
    }

    /**
     * @param topDeptId 顶级部门ID
     * @param regionId  区域ID
     * @param deptId    部门ID
     * @return
     * @Function 获取最近5条的数据采集操作
     * @FileName BatchLogServiceImpl.java
     * @MethodName getLastUpload
     * @Date 2018年6月22日 下午3:44:46
     * @Author Min DongXv
     */
    @Override
    public List<Map<String, Object>> getLastUpload(Long topDeptId, Long regionId, Long deptId, int userType,Long userId) {
        return batchLogDao.getLastUpload(topDeptId, regionId, deptId, userType,userId);
    }

    @Override
    public List<Map<String, Object>> getDoubleLastUpload(Long topDeptId, Long regionId, Long deptId, int userType,Long userId) {
        return batchLogDao.getDoubleLastUpload(topDeptId, regionId, deptId, userType,userId);
    }

    @Override
    public List<Map<String, Object>> getDoubleFrLastUpload(Long topDeptId, Long regionId, Long deptId, int userType,Long userId) {
        return batchLogDao.getDoubleFrLastUpload(topDeptId, regionId, deptId, userType,userId);
    }

    /**
     * @param columns
     * @param page
     * @param userInfo 当前用户信息
     * @param tableId  数据表ID
     * @return
     * @Function 数据采集详情
     * @FileName BatchLogServiceImpl.java
     * @MethodName getBatchDetailForPage
     * @Date 2018年6月25日 上午11:52:09
     * @Author Min DongXv
     */
    @Override
    public List<Map<String, Object>> getBatchDetailForPage(List<String> columns, Page page, SysUser userInfo,
                                                           Integer tableId) {
        // 获取顶级部门ID，没有就设为本部门ID
        Long topDeptId = userInfo.getOfficeId();
        String pIds = userInfo.getAllParentId();
        if (StringUtils.isNotBlank(pIds)) {
            String t = StringUtils.split(pIds, ",")[0];
            topDeptId = StringUtils.isNoneBlank(t) ? Long.valueOf(t) : topDeptId;
        }
        String startTime = DateUtils.getDay(false, 0, DateUtils.STANDADD_PATTERN);
        String endTime = DateUtils.getDay(false, 1, DateUtils.STANDADD_PATTERN);
        return batchLogDao.getBatchDetailForPage(columns, page, topDeptId, userInfo.getAreaId(), tableId,
                userInfo.getOfficeId(), userInfo.getUserType(), startTime, endTime);
    }

    /**
     * @param data
     * @param tableId
     * @param rootId
     * @return
     * @Function 手工采集
     * @FileName BatchLogServiceImpl.java
     * @MethodName manualCollection
     * @Date 2018年6月27日 上午10:53:41
     * @Author Min DongXv
     */
    @Override
    @Transactional
    public void manualCollection(String data, Integer tableId, Integer rootId, SysUser userInfo, Integer contentId) {
        StringBuilder error = new StringBuilder();
        // 获取数据表信息
        Table table = tableService.queryCubeById(tableId);
        table.setRootId(rootId);
        // 获取数据表列信息
        // List<Column> columns = columnService.getListByTableId(tableId);
        Long topDeptId = userInfo.getOfficeId();
        String pIds = userInfo.getAllParentId();
        if (StringUtils.isNotBlank(pIds)) {
            String t = StringUtils.split(pIds, ",")[0];
            topDeptId = StringUtils.isNoneBlank(t) ? Long.valueOf(t) : topDeptId;
        }

        String tipId = tableId + "_" + topDeptId;
        TableResult table1 = contentService.getTableColumns(tableId, contentId, tipId);
        List<Column> columns = table1.getColumns();
        String batchId = UuidUtil.getInstance().getUUid();
        // 新增批次信息
        batchLogDao.addBatchLog(batchId.toString(), tableId, "2", rootId, userInfo.getOfficeId(), userInfo.getId(),
                LogEnum.CLZ, null);
        // 获取批次详细信息
        BatchLog batchLog = batchLogDao.select(batchId);
        List<Map<String, Object>> listDataMap = JSONObject.parseObject(data.replaceAll(" ", ""),
                new TypeReference<List<Map<String, Object>>>() {
                });
        List<Map<String, Object>> listDataMap2 = (List<Map<String, Object>>) JSONArray.parse(JSONArray.toJSONString(listDataMap));
        for(Map<String, Object> map : listDataMap) {
        	if(table.getTableId()==210001){				
	        	String A000001 = CleanData(map.get("A000001").toString()).replaceAll("\\(", "（").replaceAll("\\)", "）");
				map.put("A000001", A000001);
	        	String CF_WSH = CleanData(map.get("CF_WSH").toString()).replaceAll("\\[", "〔").replaceAll("\\]", "〕").
	        			replaceAll("【", "〔").replaceAll("】", "〕").
	        			replaceAll("﹝", "〔").replaceAll("﹞", "〕").
	        			replaceAll("［", "〔").replaceAll("］", "〕").
	        			replaceAll("\\{", "〔").replaceAll("\\}", "〕");
				map.put("CF_WSH", CF_WSH);
	        	
			}else if(table.getTableId()==210003){		
				String B000001 = CleanData(map.get("B000001").toString()).replaceAll("\\(", "（").replaceAll("\\)", "）");
				map.put("B000001", B000001);
	        	String CF_WSH = CleanData(map.get("CF_WSH").toString()).replaceAll("\\[", "〔").replaceAll("\\]", "〕").
	        			replaceAll("【", "〔").replaceAll("】", "〕").
	        			replaceAll("﹝", "〔").replaceAll("﹞", "〕").
	        			replaceAll("［", "〔").replaceAll("］", "〕").
	        			replaceAll("\\{", "〔").replaceAll("\\}", "〕");
				map.put("CF_WSH", CF_WSH);
	        	
			}else if(table.getTableId()==210002) {		
				String B000001 = CleanData(map.get("B000001").toString()).replaceAll("\\(", "（").replaceAll("\\)", "）");
				map.put("B000001", B000001);
				String XK_WSH = CleanData(map.get("XK_WSH").toString()).replaceAll("\\[", "〔").replaceAll("\\]", "〕").
	        			replaceAll("【", "〔").replaceAll("】", "〕").
	        			replaceAll("﹝", "〔").replaceAll("﹞", "〕").
	        			replaceAll("［", "〔").replaceAll("］", "〕").
	        			replaceAll("\\{", "〔").replaceAll("\\}", "〕");
				map.put("XK_WSH", XK_WSH);
				
			}else if(table.getTableId()==210000) {				
				String A000001 = CleanData(map.get("A000001").toString()).replaceAll("\\(", "（").replaceAll("\\)", "）");
				map.put("A000001", A000001);
				String XK_WSH = CleanData(map.get("XK_WSH").toString()).replaceAll("\\[", "〔").replaceAll("\\]", "〕").
	        			replaceAll("【", "〔").replaceAll("】", "〕").
	        			replaceAll("﹝", "〔").replaceAll("﹞", "〕").
	        			replaceAll("［", "〔").replaceAll("］", "〕").
	        			replaceAll("\\{", "〔").replaceAll("\\}", "〕");
				map.put("XK_WSH", XK_WSH);
				
			}
        }
        
        int size = listDataMap.size();
        // 处理流程日志
        stepLogService.addStepLog(batchId, "解析手工采集到的" + size + "条数据。");
        // 重复性校验
        stepLogService.addStepLog(batchId, "开始重复性校验：0到" + size + "行数据。");

        // 数据是否需要重复性校验
        boolean checkRepeat = true;
        if (CHECKREPEAT_NO.equals(table.getRepeatCheck())) {
            checkRepeat = false;
        }
        // 数据重复性校验
        Map<String, Object> duplicationCheck = inspectDataStyleService.duplicationCheck(listDataMap, table, columns,
                false, checkRepeat);
        String duplicationMess = String.valueOf(duplicationCheck.get("duplicationMess"));
        if (null != duplicationCheck && StringUtils.isNotEmpty(duplicationMess)) {
            error.append(duplicationMess);
            // 通过抛出异常执行回滚
            throw new ServiceException(error.toString());
        }
        stepLogService.addStepLog(batchId, "开始校验手工采集的0到" + size + "行数据");
        // 数据格式校验
        Map<String, Object> styleCheck = inspectDataStyleService.styleCheck(batchLog, listDataMap, table, columns,listDataMap2);
        String styleMess = String.valueOf(styleCheck.get("mess"));
        if (null != styleCheck && StringUtils.isNotEmpty(styleMess)) {
            error.append(styleMess);
            // 通过抛出异常执行回滚
            throw new ServiceException(error.toString());
        }
        
        // 数据校验
        for (Map<String, Object> map : listDataMap) {
        	String errorMsg = inspectDataStyleService.dataCheck(table.getTableId(),map,columns);
        	if(null != errorMsg && StringUtils.isNotEmpty(errorMsg)) {
        		throw new ServiceException(errorMsg.toString());
        	}
		}
        stepLogService.addStepLog(batchId, "开始将手工采集的0到" + size + "行数据录入至清洗库。");
        dataEntryService.addSuccessData(listDataMap, table, batchLog, columns);
        stepLogService.addStepLog(batchId, "结束将手工采集的0到" + size + "行数据录入至清洗库。");
        // 通过抛出异常执行回滚
        if (StringUtils.isNotEmpty(error)) {
            throw new ServiceException(error.toString());
        } else {
            // 未异常更新本次数据数量
            int errorCount = Integer.parseInt(
                    null != styleCheck.get("errorCount") ? String.valueOf(styleCheck.get("errorCount")) : "0");
            int duplicationCount = Integer.parseInt(null != duplicationCheck.get("duplicationCount")
                    ? String.valueOf(duplicationCheck.get("duplicationCount")) : "0");
            batchLogDao.modifyBatchLog(batchId, LogEnum.EX_RESULT_S, size, size - errorCount - duplicationCount,
                    errorCount, duplicationCount, 0, LogEnum.YWC);
        }
    }

    @Override
    public void updateBatchLogWaitCount(String batchId, int waitCount) {
        batchLogDao.updateBatchLogWaitCount(batchId, waitCount);
    }

    @Override
    public boolean isProcessing(String status) {

        return batchLogDao.selectCountByAssociation(status) > 0;
    }

    @Override
    public List<String> getBatchIdByOfficeId(long officeId) {

        return batchLogDao.selectBatchIdByOfficeId(officeId);
    }

	@Override
	public Map<String, Object> selectTotal(SysUser userInfo) {
		  Long deptId = userInfo.getOfficeId();
		  String rolename = userInfo.getRoleName();
		  Long userId =userInfo.getId();
		  Map<String, Object> map = new HashMap<String, Object>();
		  if(rolename.equals("系统管理员")) {
			  map = batchLogDao.selectTotal(null,null);  
		  }else if(rolename.equals("部门管理员")) {
			  map = batchLogDao.selectTotal(deptId,null );  
		  }else {
			  map  = batchLogDao.selectTotal(deptId,userId);  
		  }
	       
	        return map;
	}
	@Transactional
	@Override
	public void FrjlAutoPush() {
		//将数据推送
		 batchLogDao.RedBlackAutoPush();
		 List sqllist = new ArrayList();
		 sqllist.add("update t_1000000010051_NnZ50yNEjJ set is_push=1");
		 sqllist.add("update t_100000470021_7Q4CFxTJWS set is_push=1");		
		 //原始数据推送状态改变
		 for(int i=0;i<sqllist.size();i++) {
			 batchLogDao.updatebaseInfo(sqllist.get(i).toString()); 
		 }
	}

	@Override
	public void FrcjAutoPush() {
		//将数据推送
		 batchLogDao.FrcjAutoPush();
		 List sqllist = new ArrayList();
		 sqllist.add("update t_1000000010048_ZdSrbj13gI set is_push=1");
		 sqllist.add("update t_100000480022_l7l5N1T4Y1 set is_push=1");
		 sqllist.add("update t_100000470024_AQjUiBpBlP set is_push=1");
		 //原始数据推送状态改变
		 for(int i=0;i<sqllist.size();i++) {
			 batchLogDao.updatebaseInfo(sqllist.get(i).toString()); 
		 }
		
		
	}

	@Override
	public void ZrrcjAutoPush() {
		//将数据推送
		 batchLogDao.ZrrcjAutoPush();
		 List sqllist = new ArrayList();
		 sqllist.add("update t_1000010010053_AkVFirDyoZ set is_push=1");
		 sqllist.add("update t_1000010010057_MJIIc7qcCo set is_push=1");
		 //原始数据推送状态改变
		 for(int i=0;i<sqllist.size();i++) {
			 batchLogDao.updatebaseInfo(sqllist.get(i).toString()); 
		 }
	}

	@Override
	public void ZrrjlAutoPush() {
		//将数据推送
		 batchLogDao.ZrrjlAutoPush();
		 List sqllist = new ArrayList();
		 sqllist.add("update t_1000010010054_0uzcoNqMzg set is_push=1");
		 //原始数据推送状态改变
		 for(int i=0;i<sqllist.size();i++) {
			 batchLogDao.updatebaseInfo(sqllist.get(i).toString()); 
		 }
	}

	
	/* (非 Javadoc)
	 *
	 
	 *
	 
	 * @see com.py.credit.service.BatchLogService#UpdateCollectionState()
	 */
	
	@Override
	public void UpdateCollectionState() {
	
		 batchLogDao.UpdateCollectionState(); 
		
	}
	public static String CleanData(String data) {
		String repl = "";
		if(StringUtils.isNotBlank(data)) {
			Pattern p = Pattern.compile("\\s*|\t|\r|\n");
            Matcher m = p.matcher(data);
            repl = m.replaceAll("").replaceAll("[?]", "").replaceAll("\\?", "");
            Matcher matcher = Pattern.compile("[a-z]").matcher(repl);
            while(matcher.find()){
            	repl = repl.replace(matcher.group(), matcher.group().toUpperCase());
            }
          
		}
		return repl;
		
	}

	@Override
	public List<Map<String, Object>> getTenLastUpload(Long topDeptId, Long regionId, Long deptId, int userType,Long userId) {
		   return batchLogDao.getTenLastUpload(topDeptId, regionId, deptId, userType,userId);
	}

	@Override
	public List<Map<String, Object>> getTenByConditionForPage(List<String> columns, String[] additionalColumns,
			String condition, Page page, SysUser userInfo) {
		 List<String> conditions = SQLBuilder.createConditionSql(condition);
	        Long topDeptId = userInfo.getOfficeId();
	        String pIds = userInfo.getAllParentId();
	        if (StringUtils.isNotBlank(pIds)) {
	            String t = StringUtils.split(pIds, ",")[0];
	            topDeptId = StringUtils.isNoneBlank(t) ? Long.valueOf(t) : topDeptId;
	        }
	        String roleName = userInfo.getRoleName();
	        List<Map<String, Object>> batchLogs = new ArrayList<Map<String,Object>>();
	        if (roleName.equals("系统管理员")) {
	            batchLogs  = batchLogDao.selectTenByConditionForPage(columns, additionalColumns,
	                    conditions, page, topDeptId, null, userInfo.getUserType(), null);
	        }else if(roleName.equals("部门管理员")) {
	            batchLogs  = batchLogDao.selectTenByConditionForPage(columns, additionalColumns,
	                    conditions, page, topDeptId, null, userInfo.getUserType(), userInfo.getOfficeId());
	        }else {
	            batchLogs  = batchLogDao.selectTenByConditionForPage(columns, additionalColumns,
	                    conditions, page, topDeptId, userInfo.getId(), userInfo.getUserType(), userInfo.getOfficeId());
	        }
	        return batchLogs;
	}
	
	
	
}
